/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 *
 * @author jeovane
 */
public class JDOMPrefixes {

    private SAXBuilder builder = new SAXBuilder();
    private Document document;
    private Element root;

    public JDOMPrefixes(File file) {
        try {
            this.document = builder.build(file);
            this.root = document.getRootElement();
        } catch (JDOMException ex) {
            System.out.println("JDOMException: " + ex);
        } catch (IOException ex) {
            System.out.println("IOException: " + ex);
        }
    }

    public Map<String, String> getPrefixesForXML() throws JDOMException, IOException {
        List<Element> rowPrefixes = root.getChildren("prefixes");
        Map<String, String> mapGetPrefixes = new HashMap<String, String>();

        for (int i = 0; i < rowPrefixes.size(); i++) {
            Element prefixes = rowPrefixes.get(i);
            List prefixColumn = prefixes.getChildren();

            for (int j = 0; j < prefixColumn.size(); j++) {
                Element prefix = (Element) prefixColumn.get(j);
                String name = "PREFIX " + prefix.getAttribute("name").getValue() + ":";
                String nameText = "<" + prefix.getText() + ">";
                mapGetPrefixes.put(name, nameText);
            }
        }

        return mapGetPrefixes;
    }

    public Map<String, String> getServicesForXML() throws JDOMException, IOException {
        List<Element> rowServices = root.getChildren("services");
        Map<String, String> mapGetServices = new HashMap<String, String>();

        for (int i = 0; i < rowServices.size(); i++) {
            Element prefixes = rowServices.get(i);
            List prefixColumn = prefixes.getChildren();

            for (int j = 0; j < prefixColumn.size(); j++) {
                Element prefix = (Element) prefixColumn.get(j);
                String name = prefix.getAttribute("name").getValue();
                String nameText = prefix.getText();
                mapGetServices.put(name, nameText);
            }
        }

        return mapGetServices;
    }

    public Set<TripleXML> getPropertiesForXML() {
        List<Element> rowProperties = root.getChildren("properties");
        Set<TripleXML> properties = new HashSet<TripleXML>();
        TripleXML tripleXML;

        for (int i = 0; i < rowProperties.size(); i++) {
            Element property = rowProperties.get(i);
            List propertyColumn = property.getChildren();

            for (int j = 0; j < propertyColumn.size(); j++) {
                Element binding = (Element) propertyColumn.get(j);
                List bindingProperty = binding.getChildren();
                String nameProperty = binding.getAttribute("name").getValue();

                for (int k = 0; k < bindingProperty.size(); k++) {
                    Element bindingName = (Element) bindingProperty.get(k);
                    tripleXML = new TripleXML();

                    String sonBinding = bindingName.getChildren().get(0).getText().toString();
                    String name = bindingName.getAttribute("name").getValue();

                    tripleXML.setClazz(nameProperty);
                    tripleXML.setProperty(sonBinding);
                    tripleXML.setPrefix(name);

                    properties.add(tripleXML);
                }
            }
        }

        return properties;
    }

    public static void main(String[] args) throws JDOMException, IOException {
        File file = new File("/home/jeovane/Dropbox/NetBeansProjects/JBOS/JBOS/web/prefixes.xml");
        JDOMPrefixes prefixes = new JDOMPrefixes(file);
        Set<String> setPrefix = new HashSet<String>();
        setPrefix.add("sider");
        setPrefix.add("dailymed");

//        for (Iterator it = prefixes.getServicesForXML().entrySet().iterator(); it.hasNext();){
//            Map.Entry entry = (Map.Entry) it.next();
//            System.out.println(entry.getKey() + " " + entry.getValue());
//        }

//        for (TripleXML tripleXML : prefixes.getPropertiesForXML()){
//            System.out.println(tripleXML.getClazz() + " - " + tripleXML.getProperty() + " - " + tripleXML.getPrefix());
//        }

        for (String prefix : setPrefix) {
            if (prefixes.getServicesForXML().containsKey(prefix)) {
                System.out.println(prefixes.getServicesForXML().get(prefix));
            }
        }
    }
}
